<?php

//
// +------------------------------------------------------------------------+
// | PHP Version 5.4                                                        |
// +------------------------------------------------------------------------+
// | Copyright (c) All rights reserved.                                     |
// +------------------------------------------------------------------------+
// | 世纪车来车往(北京）网络科技有限公司 版权所有！                              |
// | TEL:010-63732365  HOTLINE:400-6688-365  EMAIL:clcw@clcw.com.cn         |
// | $Revision: 1.0 $                                                       |
// +------------------------------------------------------------------------+
// | Author: anguolei@clcw.com.cn                                           |
// +------------------------------------------------------------------------+
//

namespace AuctionBack\Controller;

use AuctionCore\System\SystemConf;
use AuctionCore\Common\AjaxPage;


class SettingController extends BaseController
{

    public function __construct()
    {
        parent::__construct();

    }

    public function index()
    {
        $this->display();
    }

    public function get_list()
    {
        $page = I("get.p/d", 1);
        $limit = 10;
        
        $result = SystemConf::getList($page, $limit);
        $total = $result['total'] > 0 ? $result['total'] : 0;
        $list = $result['list'] > 0 ? $result['list'] : [];
        $Page = new AjaxPage($total, $limit, "get_page");
        $Page->setConfig('theme', " %totalRow% %header% %nowPage%/%totalPage% 页 %first% %upPage% %prePage% %linkPage% %downPage% %nextPage% %end% %jumpform%");
        $page_show = $Page->show();

        $this->assign('p', $page);
        $this->assign('page', $page_show);
        $this->assign('list', $list);
        $this->display();
    }

    public function add_setting()
    {
        $title = I("post.title");
        if(empty($title)){
            $this->helpJsonResult(400,"标题不能为空");
        }
        $name = I("post.name");
        if(empty($title)){
            $this->helpJsonResult(400,"名称不能为空");
        }
        $is_exists = SystemConf::getConfByName($name);
        if(!empty($is_exists)){
            $this->helpJsonResult(400,"配置名称已存在");
        }
        $remark = I("post.remark");
        if(empty($title)){
            $this->helpJsonResult(400,"配置说明不能为空");
        }
        $value = I("post.value");
        if(empty($title)){
            $this->helpJsonResult(400,"配置值不能为空");
        }
        $data = array(
            'title'=>trim($title),
            'name'=>trim($name),
            'remark'=>trim($remark),
            'value'=>trim($value),
        );

        $ret = SystemConf::create($data);
        if(!$ret){
            $this->helpJsonResult(500,"添加失败");
        }
        $this->helpJsonResult(200,"添加成功");
    }

    public function edit()
    {
        $id = I("get.id/d", 0);
        $conf = new SystemConf($id);
        $detail = $conf->detail();
        $this->assign("detail", $detail);
        $this->display();
    }

    public function update()
    {
        $id = I("post.id/d", 0);
        if($id<1){
            $this->helpJsonResult(400,"参数有误");
        }
        $title = I("post.title");
        if(empty($title)){
            $this->helpJsonResult(400,"标题不能为空");
        }
        $name = I("post.name");
        if(empty($title)){
            $this->helpJsonResult(400,"名称不能为空");
        }
        $is_exists = SystemConf::getConfByName($name);
        if(!empty($is_exists) && $is_exists['id']!=$id){
            $this->helpJsonResult(400,"配置名称已存在");
        }
        $remark = I("post.remark");
        if(empty($title)){
            $this->helpJsonResult(400,"配置说明不能为空");
        }
        $value = I("post.value");
        if(empty($title)){
            $this->helpJsonResult(400,"配置值不能为空");
        }
        $data = array(
            'title'=>trim($title),
            'name'=>trim($name),
            'remark'=>trim($remark),
            'value'=>trim($value),
        );
        $setting = new SystemConf($id);
        $ret = $setting->update($data);
        if(!$ret){
            $this->helpJsonResult(500,"数据未更新");
        }
        $this->helpJsonResult(200,"更新成功");
    }
}
